package com.wkw.jkyf.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wkw.jkyf.common.ResultMapUtil;
import com.wkw.jkyf.pojo.Supplier;
import com.wkw.jkyf.pojo.User;
import com.wkw.jkyf.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {

  @Resource
  private IUserService userService;


  /*
  转向登录页面
   */

  @RequestMapping(value = "login")
    public String login(){
      return "/login";
  }


  //判断用户登录是否成功
  @RequestMapping(value = "toLogin")
  @ResponseBody
  public Object toLogin(String username,String password){
    if(username == null || password == null){
      return ResultMapUtil.getHashMapLogin("用户密码不能为空","2");
    }
    //获取当前的用户
    Subject subject = SecurityUtils.getSubject();
    //封装用户的登录数据
    UsernamePasswordToken token =new UsernamePasswordToken(username,password);
    try{
      //执行登录的方法，如果没有异常就说明成功了！
      subject.login(token);
      subject.getSession().setAttribute("userName",username);
    }catch (UnknownAccountException e){//用户名不存在
      return ResultMapUtil.getHashMapLogin("用户名不存在","2");
    }catch (IncorrectCredentialsException e){//密码不存在
      return ResultMapUtil.getHashMapLogin("密码错误","2");
    }
    return ResultMapUtil.getHashMapLogin("验证成功","1");
  }


  /*
  转向后台管理首页
   */
  @RequestMapping(value = "/index")
  public String index(){
    return "/index";
  }
  /*
   退出登录
   */
  @RequestMapping(value = "/logout")
  public String logout(){
    //获取当前的用户
    Subject subject = SecurityUtils.getSubject();
    subject.logout();
    return "redirect:/login";
  }

  //分页查询用户列表
  @RequestMapping("/userQueryPage")
  @ResponseBody
  public Object userQueryPage(String param, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int pageSize){
    try{
      IPage<User> iPage = userService.selectUserPage(page,pageSize,param);
      return ResultMapUtil.getHashMapMysqlPage(iPage);
    }catch (Exception e){
      return ResultMapUtil.getHashException(e);
    }
  }
  //跳转到添加页面
  @RequestMapping(value = "/userPage")
  public String userPage(){
    return "/userPage";
  }

 //跳转到用户页面
  @RequestMapping(value = "/user")
  public String user(){
    return "/user";
  }

  //添加
  @RequestMapping(value = "/userAdd")
  @ResponseBody
  public Object userAdd(User user){
    try {
      int i = userService.addUser(user);
      return ResultMapUtil.getHashMapSave(i);
    } catch (Exception e) {
      return ResultMapUtil.getHashException(e);
    }
  }

  //按id查询
  @RequestMapping(value = "/userQueryById")
  public String userQueryById(@RequestParam(name = "id",required = true)Integer id, Model model) {
    User user = userService.queryUserById(id);
    model.addAttribute("obj",user);
    return "/userPage";
  }

  //修改
  @RequestMapping(value = "/userEdit")
  @ResponseBody
  public Object userEdit(User user) {
    try {
      int i= userService.editUser(user);
      return ResultMapUtil.getHashMapSave(i);
    } catch (Exception e) {
      return  ResultMapUtil.getHashException(e);
    }
  }

  // 删除
  @RequestMapping(value = "/userDelById")
  @ResponseBody
  public Object userDelById(Integer id) {
    try {
      int i= userService.deleteUserById(id);
      return ResultMapUtil.getHashMapDel(i);
    } catch (Exception e) {
      return  ResultMapUtil.getHashException(e);
    }
  }

  //获取所有用户
  @RequestMapping(value = "/userList")
  @ResponseBody
  public Object userList(){
    List<User> userList = userService.queryUserList();
    return ResultMapUtil.getHashMapMyList(userList);
  }
}
